package com.pub.functions;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzhonglong
 * Date: 2008-8-5
 * Time: 20:02:06
 */
public class ColFuncs {

	public static <T> boolean notEmpty(List<T> list) {
		return list != null && list.size() > 0;
	}

	public static <T> boolean notEmpty(T[] arr)
	{
		return arr != null && arr.length > 0;
	}

	public static <T> List<T> toList(T[] arr) {
		return Arrays.asList(arr);
	}

	public static Integer[] StringArrayToIntegerArray(String[] arr) {
		Integer[] result = new Integer[arr.length];
		for(int n = 0; n < arr.length; n++) {
			result[n] = Integer.valueOf(arr[n]);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] compactArray(T[] arr) {
		List<T> list = new ArrayList<T>();
		for(T t: arr) {
			if(t != null) {
				list.add(t);
			}
		}
		if(list.size() == arr.length) {
			return arr;
		}
		arr = (T[]) Array.newInstance(arr.getClass().getComponentType(), 0);
		return list.toArray(arr);
	}

}
